<template>
  <div>
    <h1>商品类型</h1>
  </div>
  <div>
    <el-button size="small" type="primary" @click="onAddType"
      >添加类型</el-button
    >
  </div>
  <div>
    <el-table
      :data="typeTree"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="name" label="名称" width="180" />
      <el-table-column prop="typeImage" label="图片" width="100">
        <template #default="scope">
          <el-image
            v-if="scope.row.typeImage != null"
            style="width: 50px; height: 50px"
            :src="scope.row.typeImage"
          />
        </template>
      </el-table-column>
      <el-table-column prop="typeStatus" label="状态" width="100">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.typeStatus == 0">显示</el-tag>
          <el-tag type="danger" v-if="scope.row.typeStatus == 1">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="typeTop" label="排序" width="100" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">
            编辑
          </el-button>

          <el-button
            type="primary"
            v-if="scope.row.typeStatus == 0"
            size="small"
            @click="handleCheck(scope.row, 1)"
          >
            隐藏
          </el-button>

          <el-button
            type="primary"
            v-if="scope.row.typeStatus == 1"
            size="small"
            @click="handleCheck(scope.row, 0)"
          >
            显示
          </el-button>

          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>

          <el-button size="small" type="primary" @click="onAddTypeC(scope.row)">
            添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="dialogFormVisible" :title="title" width="500">
      <el-form :model="form">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.typeName" autocomplete="off" />
        </el-form-item>

        <el-form-item label="分类图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:10010/shop/type/uploadFile"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="headers"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input v-model="form.typeTop" autocomplete="off" />
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.typeStatus">
            <el-radio :value="0">显示</el-radio>
            <el-radio :value="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">放弃</el-button>
          <el-button
            type="primary"
            v-if="title == '添加类型'"
            @click="onsubmitAddType"
          >
            提交
          </el-button>
          <el-button
            type="primary"
            v-if="title != '添加类型'"
            @click="onsubmitUpdate"
          >
            修改
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import {
  getTypeTree,
  setTypeStatus,
  delTypeById,
  addType,
  updateType,
} from "../api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";

const imageUrl = ref("");
const headers = {
  token: sessionStorage.getItem("token"),
};

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = response.data;
  form.value.typeImage = imageUrl.value;
};
const typeTree = ref([]);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const title = ref("添加类型");

const form = ref({
  typeImage: null,
  typeName: null,
  typePid: 0,
  typeStatus: 0,
  typeTop: 0,
});

const ResultFrom = () => {
  form.value = {
    typeImage: null,
    typeName: null,
    typePid: 0,
    typeStatus: 0,
    typeTop: 0,
  };
  imageUrl.value = "";
};

const loadListTree = () => {
  getTypeTree().then((res) => {
    if (res.data.code == 200) {
      typeTree.value = res.data.data;
    }
  });
};

const handleEdit = (row: any) => {
  console.log(row);
  title.value = "编辑分类";
  Object.assign(form.value, row);
  form.value.typeName = row.name;
  form.value.typeId = row.id;
  imageUrl.value = form.value.typeImage;
  form.value.typePid = row.parentId;
  dialogFormVisible.value = true;
};

//修改
const onsubmitUpdate = () => {
  form.value.typeImage = imageUrl.value;
  updateType(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      dialogFormVisible.value = false;
      loadListTree();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//根据行内ID 状态修改
const handleCheck = (row: any, typeStatus: any) => {
  console.log(row);
  setTypeStatus(row.id, typeStatus).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("状态改变成功");
      loadListTree();
    }
  });
};

//单删消息对话框
const handleDelete = (row: any) => {
  console.log(row);
  ElMessageBox.confirm("删除后信息无法恢复，确定删除吗?", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "放弃",
    type: "warning",
  })
    .then(() => {
      //单删根据ID删除
      delTypeById(row.id).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadListTree();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {});
};

//父子级分类添加
const onsubmitAddType = () => {
  title.value = "添加类型";
  addType(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      loadListTree();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//添加父级类型对话框
const onAddType = () => {
  ResultFrom();
  dialogFormVisible.value = true;
  title.value = "添加类型";
  //添加一级分类
  form.value.typePid = 0;
};

//添加子级类型对话框
const onAddTypeC = (row: any) => {
  ResultFrom();
  //添加子级分类
  form.value.typePid = row.id;
  dialogFormVisible.value = true;
};

//页面加载钩子函数
onMounted(() => {
  loadListTree();
});
</script>

<style scoped></style>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
